/**
 * 
 */
package fr.sportcom.sportcom.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONObject;

/**
 * @author Xavier
 *
 */
public class CustomHttpClient {
	
	public static final int HTTP_TIMEOUT = 2 * 1000; // milliseconds
	/** Single instance of our HttpClient */
	private static HttpClient mHttpClient;
	
	// DEV
	public static final String HTTP_URL = "http://dev.sportincom.fr/?f=";
	public static final String HTTP_BASE_URL = "http://dev.sportincom.fr/";
	// REC
	//public static final String HTTP_URL = "http://rec.sportincom.fr/?f=";
	//public static final String HTTP_BASE_URL = "http://rec.sportincom.fr/";
	// Test
	//public static final String HTTP_URL = "http://test.sportincom.fr/?f=";
	//public static final String HTTP_BASE_URL = "http://test.sportincom.fr/";
	// Prod
	//public static final String HTTP_URL = "http://srvapp.sportincom.fr/?f=";
	//public static final String HTTP_BASE_URL = "http://srvapp.sportincom.fr/";
		
	/**
	 * Get our single instance of our HttpClient object.
	 *
	 * @return an HttpClient object with connection parameters set
	 */
	private static HttpClient getHttpClient() {
		if (mHttpClient == null) {
			mHttpClient = new DefaultHttpClient();
			final HttpParams params = mHttpClient.getParams();
			HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
			HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
			ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
		}
		return mHttpClient;
	}
	
	public JSONObject postData(String funcName, ArrayList<NameValuePair> post) throws Exception {
		// On cr�� un client http
		HttpClient httpclient = getHttpClient();
		// On cr�� notre ent�te
		HttpPost httppost = new HttpPost(HTTP_URL + funcName);
		// Ajoute la liste � notre ent�te
		if(post != null) {
			httppost.setEntity(new UrlEncodedFormEntity(post));
		}

		// On ex�cute la requ�te tout en r�cup�rant la r�ponse
		HttpResponse response = httpclient.execute(httppost);

		BufferedReader in = null;
		StringBuffer sb = null;
		try {
			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
			sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
		}
		finally {
			try {
				in.close();
			} 
			catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		// Parsing du JSON en list.
		JSONObject jObj = new JSONObject(sb.toString());
		return jObj;
	}
	
	public JSONObject postData(String funcName) throws Exception {
		return postData(funcName, null);
	}
}
